package com.example.restatr.Atcivity.Park;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyCarAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private ListView list;
    private FloatingActionButton fab;
    private LinearLayout Null;
    private List<String> type = new ArrayList<>();
    private List<String> clId = new ArrayList<>();
    private List<String> plateNo = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_wdcl_at );

        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        list = findViewById( R.id.list );
        fab = findViewById( R.id.fab );
        Null = findViewById( R.id.Null );

        Tools.SetBarTitle( tb, barTitle, "我的车辆", search, MyCarAt.this,false );


        fab.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent( MyCarAt.this, SetCarInfoAt.class );
                intent.putExtra( "mold", "add" );
                startActivity( intent );

            }
        } );
        list.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent( MyCarAt.this, SetCarInfoAt.class );
                intent.putExtra( "mold", "edit" );
                intent.putExtra( "type", type.get( position ) );
                intent.putExtra( "platNo", plateNo.get( position ) );
                intent.putExtra( "id", clId.get( position ) );
                startActivity( intent );
            }
        } );
    }

    class WdclListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return plateNo.size();
        }

        @Override
        public Object getItem(int position) {
            return plateNo.get( position );
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate( R.layout.address_list_item, null );
            TextView labeled = convertView.findViewById( R.id.labeled );
            TextView address = convertView.findViewById( R.id.address );
            TextView name = convertView.findViewById( R.id.name );
            TextView phone = convertView.findViewById( R.id.phone );
            ImageView editAddress = convertView.findViewById( R.id.edit_address );


            address.setText( plateNo.get( position ) );
            labeled.setText( type.get( position ) );

            name.setVisibility( View.GONE );
            phone.setVisibility( View.GONE );
            editAddress.setVisibility( View.GONE );


            return convertView;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        type.clear();
        clId.clear();
        plateNo.clear();
        new Thread( () -> {
            try {
                String token = Tools.Get( "/prod-api/api/park/car/my", MainActivity.sp.getString( "token", "" ) );
                JSONObject all = new JSONObject( token );
                JSONArray rows = all.getJSONArray( "rows" );
                if (rows.length()>0){
                    for (int i = 0; i < rows.length(); i++) {
                        JSONObject jx = rows.getJSONObject( i );
                        type.add( jx.getString( "type" ) );
                        clId.add( jx.getString( "id" ) );
                        plateNo.add( jx.getString( "plateNo" ) );
                    }
                }
                runOnUiThread( () -> {
                    if (plateNo.size()>0) {
                        list.setVisibility( View.VISIBLE );
                        list.setAdapter( new WdclListAdapter() );
                        Null.setVisibility( View.GONE );
                    } else {
                        Null.setVisibility( View.VISIBLE );
                        list.setVisibility( View.GONE );
                    }
                } );
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } ).start();
    }
}